<template>
  <div class="level">
    <h1>医院</h1>
    <div class="content">
      <div class="left">等级：</div>
      <ul>
        <li :class="{ active: levelValue == '' }" @click="changeLevel('')">
          全部
        </li>
        <li
          v-for="item in levelArr"
          :key="item.value"
          @click="changeLevel(item.value)"
          :class="{ active: levelValue === item.value }"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts" name="Level">
import { reqHospitalLevelOrRegion } from '@/api/home'
import type { HospitalLevelAndRegionArr } from '@/api/home/type'
import { ElMessage } from 'element-plus'
import { onMounted, ref } from 'vue'

// 存储医院等级
let levelArr = ref<HospitalLevelAndRegionArr>([])
// 当前点击医院等级的值
let levelValue = ref('')
// 声明父组件绑定的自定义事件
const emit = defineEmits(['get-level'])

// 获取医院等级的方法
const getLevel = async () => {
  let result = await reqHospitalLevelOrRegion('Hostype')
  if (result.code === 200) {
    levelArr.value = result.data
  } else {
    ElMessage.error('获取医院等级失败')
  }
}
// 点击医院等级的回调
const changeLevel = (level: string) => {
  // 修改当前点击的医院的等级值进行高亮显示
  levelValue.value = level
  // 触发自定义事件将level值传递给父组件去获取对应等级医院的数据
  emit('get-level', level)
}

onMounted(() => {
  // 获取医院等级
  getLevel()
})
</script>

<style scoped lang="scss">
.level {
  color: #7f7f7f;
  h1 {
    margin-top: 10px;
    font-size: 18px;
  }
  .content {
    margin: 20px 0;
    display: flex;
    .left {
      margin-right: 8px;
    }
    ul {
      display: flex;
      li {
        margin-right: 20px;
        &.active {
          color: $base-active-color;
        }
        &:hover {
          color: $base-active-color;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
